﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Psl.Applications;
using Psl.Chat;

namespace Stl.Chat.Services.Plugin
{
    public partial class ChatStatistic : UserControl
    {

        /************************************************************
         *                          Champs                          *
         ************************************************************/
        private long time = 0;
        private int msg = 0;


        /************************************************************
         *                        Constructeur                      *
         ************************************************************/
        public ChatStatistic()
        {
            InitializeComponent();

            // merge le menu dans registry
            Registry.MergeInMainMenu(menuStrip);
            Registry.MergeInMainTools(toolStrip);

            // set interval du timer
            timer.Interval = 500;

            // action
            restartAction.Enabled = false;
        }


        /************************************************************
         *                         Evenements                       *
         ************************************************************/
        private void timer_Tick(object sender, EventArgs e)
        {
            doUpdate();
        }

        private void chatEvents_ChatOpened(object sender, EventArgs e)
        {
            Start();
            restartAction.Enabled = true;
        }

        private void chatEvents_ChatClosing(object sender, EventArgs e)
        {
            restartAction.Enabled = false;
            statusLabel.Text = ChatRegistry.ChatState.Status.ToString();
            Stop();
        }

        private void chatEvents_ChatClosed(object sender, EventArgs e)
        {
            statusLabel.Text = ChatRegistry.ChatState.Status.ToString();
        }

        private void chatEvents_ChatChanged(object sender, ChatChangedEventArgs e)
        {
            if (ChatRegistry.ChatState.Model == null) return;
            if (e.Clear)
            {
                Stop();
                debitLabel.Text = "0";
                Start();
            }
            else
            {
                msg++;
            }
        }

        private void setIntervalAction_Execute(object sender, EventArgs e)
        {
            using (InputBox input = new InputBox("Statistiques",
                                                                          "Veuillez entrer un nombre entier supérieur ou égale à zero."))
            {
                if (input.ShowDialog() == DialogResult.Cancel)
                    return;
                string text = input.Value;
                try
                {
                    int value = int.Parse(text);
                    if (value <= 0)
                        throw new FormatException();
                    timer.Interval = value;
                    MessageBox.Show("Succes : l'interval du gestionnaire de statistiques a bien été modifié(" + text + ").");
                }
                catch (FormatException)
                {
                    MessageBox.Show("Erreur : \"" + text + "\" n'est pas un nombre entier supérieur ou égale à zéro.");
                }
            }
        }

        private void showAction_Execute(object sender, EventArgs e)
        {
            showAction.Checked = !showAction.Checked;
            this.Visible = showAction.Checked;
        }

        private void showToolsAction_Execute(object sender, EventArgs e)
        {
            showToolsAction.Checked = !showToolsAction.Checked;
            toolStrip.Visible = showToolsAction.Checked;
        }

        private void restartAction_Execute(object sender, EventArgs e)
        {
            Restart();
        }

        private void applicationEvents_Archive(IArchiver sender)
        {
            sender.PushSection("Stl.Chat.Remoting.Plugin.ChatStatistic");
            try
            {
                sender.ArchiveProperty("showToolsAction.Checked", showToolsAction, "Checked", false);
                sender.ArchiveProperty("showAction.Checked", showAction, "Checked", false);
            }
            finally { sender.PopSection(); }

            if (sender.IsReading)
            {
                toolStrip.Visible = showToolsAction.Checked;
                this.Visible = showAction.Checked;
            }
        }


        /************************************************************
         *                          Méthodes                        *
         ************************************************************/
        private void doUpdate()
        {
            time += timer.Interval;

            ChatState chat = ChatRegistry.ChatState;
            if (chat == null) return;
            usageLabel.Text = chat.Usage.ToString();
            statusLabel.Text = chat.Status.ToString();

            IChat model = chat.Model;
            if (model == null) return;
            int chrono, count;
            chrono = model.Chrono;
            count = msg;
            chronoLabel.Text = chrono.ToString();
            countLabel.Text = count.ToString();

            intervalLabel.Text = timer.Interval.ToString();
            totalLabel.Text = time.ToString();
            debitLabel.Text = (count * 1000.0 / time).ToString();
        }

        public void Start()
        {
            time = 0;
            msg = 0;
            timer.Start();
        }

        public void Stop()
        {
            timer.Stop();
        }

        public void Restart()
        {
            Stop();
            Start();
        }

        private void aboutstatistic_Execute(object sender, EventArgs e)
        {
            (new Psl.Applications.StandardAboutForm("Plugin permetant d'afficher les statistiques du chat.", "Bo CAI, Fabrice Reynaud", null)).ShowDialog();
        }
    }
}
